import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    allowedPages: [],
    authList: [],
    roleId: [],
    email: '',
    realName: ''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_allowedPages: (state, pages) => {
      state.allowedPages = pages
    },
    SET_AUTHLIST: (state, authList) => {
      state.authList = authList
    },
    SET_ROLEID: (state, roleId) => {
      state.roleId = roleId
    },
    SET_EMAIL: (state, email) => {
      state.email = email
    },
    SET_REALNAME: (state, realName) => {
      state.realName = realName
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(username, userInfo.password).then(response => {
          commit('SET_TOKEN', response.result)
          setToken(response.result)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          const data = response.result
          const roles = data.roleList.map(item => item.title)
          const roleId = data.roleList.map(item => item.roleId)
          data.roleMap = [...new Set(data.roleList.reduce((a, c) => {
            const itemArr = c.pageAuth.split(',')
            return a.concat(itemArr)
          }, []))]
          data.authListMap = [...new Set(data.roleList.reduce((a, c) => {
            const itemArr = c.authList
            return a.concat(itemArr)
          }, []))]
          commit('SET_allowedPages', data.roleMap)
          commit('SET_ROLES', roles)
          commit('SET_NAME', data.userName)
          commit('SET_AVATAR', data.avatar)
          commit('SET_AUTHLIST', data.authListMap)
          commit('SET_ROLEID', roleId)
          commit('SET_EMAIL', data.email)
          commit('SET_REALNAME', data.realName)
          resolve(response)
        }).catch(error => {
          // console.log('进入getInfo（）reject 前')
          console.log(error)
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
